package com.xinjian.jianba.controller;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse;
import com.xinjian.jianba.dto.GetStsDto;
import com.xinjian.jianba.dto.TranscodeCompleteDto;
import com.xinjian.jianba.dto.VedioDto;
import com.xinjian.jianba.dto.VideoDto;
import com.xinjian.jianba.dto.VideoUrlDto;
import com.xinjian.jianba.exception.ResponseCode;
import com.xinjian.jianba.service.ValidatorService;
import com.xinjian.jianba.service.VideoService;
import com.xinjian.jianba.utils.AliVideoUtil;
import com.xinjian.jianba.utils.JsonUtil;
import com.xinjian.jianba.utils.LogBuilder;
import com.xinjian.jianba.utils.ModuleEnum;
import com.xinjian.jianba.utils.QiNiuUtil;
import com.xinjian.jianba.utils.STSUtil;

@RestController
@RequestMapping("/api/v3/jianba/sts")
public class StsController extends AbstractController {
	private static final Logger LOGGER = LoggerFactory.getLogger(StsController.class);
	@Autowired
	private ValidatorService validatorService;
	@Autowired
	private VideoService videoService;
	@PostMapping(value = { "/getsts" })
	public String getsts() {
		try {

			GetStsDto stsDto = new GetStsDto();
			AssumeRoleResponse response = STSUtil.getSts();
			if (response != null && response.getCredentials() != null) {
				stsDto.setAccessKeyId(response.getCredentials().getAccessKeyId());
				stsDto.setSecurityToke(response.getCredentials().getSecurityToken());
				stsDto.setAccessKeySecret(response.getCredentials().getAccessKeySecret());
			}
			return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG, stsDto);
		} catch (Exception e) {
			e.printStackTrace();
			return this.responseClientJsonData(ResponseCode.ERROR, e.getMessage());
		}
	}

	@PostMapping(value = { "/videoInfo/videoPath" })
	public String getVideoUrl(@RequestBody VideoUrlDto videoUrlDto) {
		if (videoUrlDto == null) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, ResponseCode.PARAM_ERROR_MSG);
		}

		LOGGER.info(LogBuilder.build(ModuleEnum.LOCATION, "视频播放地址,参数 videoUrlDto={}"), JsonUtil.getJsonString(videoUrlDto));

		try {
			if (StringUtils.isBlank(videoUrlDto.getVideoId())) {
				return this.responseClientJsonData(ResponseCode.BAD_REQUEST, "视频id不能为空!");
			}
			VideoDto dto = AliVideoUtil.getVideoInfo(videoUrlDto.getVideoId());
			return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG, dto);
		} catch (Exception e) {
			e.printStackTrace();
			return this.responseClientJsonData(ResponseCode.ERROR, "视频转码中,请稍后再试");
		}
	}
	@PostMapping(value = { "/videoInfo/ac/videoPath" })
	public String getacVideoUrl(@RequestBody VideoUrlDto videoUrlDto) {
		if (videoUrlDto == null) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, ResponseCode.PARAM_ERROR_MSG);
		}

		LOGGER.info(LogBuilder.build(ModuleEnum.LOCATION, "视频播放地址,参数 videoUrlDto={}"), JsonUtil.getJsonString(videoUrlDto));

		try {
			if (StringUtils.isBlank(videoUrlDto.getVideoId())) {
				return this.responseClientJsonData(ResponseCode.BAD_REQUEST, "视频id不能为空!");
			}
			VideoDto dto = AliVideoUtil.getVideoInfo(videoUrlDto.getVideoId());
			return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG, dto);
		} catch (Exception e) {
			e.printStackTrace();
			return this.responseClientJsonData(ResponseCode.ERROR, "视频转码中,请稍后再试");
		}
	}
	@PostMapping("/video/path")
	public String saveVideo(@RequestBody VedioDto VedioDto){
		LOGGER.info("上传用户播放地址,VedioDto={}",JsonUtil.getJsonString(VedioDto));
		try {
			validatorService.valid(VedioDto);
		} catch (Exception e) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, e.getMessage());
		}
		videoService.saveVideoUrl(VedioDto);
		return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG);
	}
	@PostMapping("/video/back")
	public String alivodBack(HttpServletRequest request){
		String json =null;
		try {
			json = QiNiuUtil.extractedRequestToStringBuffer(request).toString();
		} catch (IOException e) {
			return this.responseClientJsonData(ResponseCode.ERROR, e.getMessage());
		}
		LOGGER.info("阿里转码完成回调接口,TranscodeCompleteDto={}",json);
		videoService.vodBack(json);
		
		
		return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG);
	}
	@PostMapping("/video/del")
	public String deleteVideo(@RequestBody VideoUrlDto videoUrlDto){
		String videoId=videoUrlDto.getVideoId();
		if(videoId==null||videoId.equals("")){
			return this.responseClientJsonData(ResponseCode.ERROR, "视频id不能为空");
		}
		int result=videoService.deleteVod(videoId);
		return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG);
	}
}
